package com.tech.koufu.ui.activity;

import java.util.ArrayList;

import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.tech.koufu.R;
import com.tech.koufu.action.StockManager;
import com.tech.koufu.model.Stock;
import com.tech.koufu.tools.PublicFuc;
import com.tech.koufu.ui.adapter.QueryStockListAdapter;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * 搜索
 * 
 * @author jl_luo
 * 
 */
public class SearchActivity extends Activity implements OnClickListener{
	private Context mContext;
	private EditText edSearchStock;
	private ImageView ivSearch;
	private ListView lv_search;
	private String key;
	ArrayList<Stock> lst = null;
	private StockManager mStockManager;
	private Handler mHandler;
	String hint;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		mContext = this;
		initView();
		initEvent();
	}
	private void initView(){
		edSearchStock = (EditText)findViewById(R.id.ed_serch_stock);
		hint = edSearchStock.getHint().toString();
		edSearchStock.setTag(hint);
//		edSearchStock.setOnFocusChangeListener(PublicFuc.onFocusAutoClearHintListener);
		ivSearch = (ImageView)findViewById(R.id.img_serch_stock);
		lv_search = (ListView)findViewById(R.id.lv_search);
		lv_search.setOnItemClickListener(onItemClick);
		findViewById(R.id.img_callback).setOnClickListener(this);
		
	}
	
	 @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(mContext);
	}
	 
	 @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(mContext);
	}
	private void initEvent(){
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) { 
				super.handleMessage(msg);
				switch (msg.what) {
				case 0x11:
					if(null!=lst && lst.size()>0){
						 QueryStockListAdapter queryStockListAdapter = new QueryStockListAdapter(getApplicationContext(), lst);
						 lv_search.setAdapter(queryStockListAdapter);
	//					 lv_search.setOnItemClickListener(queryItemClick);
						 lv_search.setVisibility(View.VISIBLE);
					}
					break;

				default:
					break;
				}
			}
		};
		mStockManager = new StockManager(getApplicationContext(), getApplication(), mHandler);
		edSearchStock.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {  
				key =  edSearchStock.getText().toString();  
				 if(key!=null && !"".equals(key.trim())){  
					 edSearchStock.setHint("");
					 new Thread(new Runnable() {
						
						@Override
						public void run() { 
							 lst = mStockManager.findStocks(key.trim());
							 mHandler.obtainMessage(0x11).sendToTarget();
						}
					}).start(); 
				 }else{
					 edSearchStock.setHint(hint);
					 lv_search.setAdapter(null);
					 lv_search.setVisibility(View.GONE);
				 }
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {  
			}
			
			@Override
			public void afterTextChanged(Editable s) {  
			}
		});
		edSearchStock.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				 
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.img_callback:
			this.finish();
			break;

		default:
			break;
		}
	}

	private OnItemClickListener onItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(SearchActivity.this,MarketDeatilActivity.class);
			intent.putExtra("stockCode", lst.get(position).code);
			intent.putExtra("stockName", lst.get(position).name);
			startActivity(intent);
			SearchActivity.this.finish();
		}
	};
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
